/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.component;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.Date;
import java.util.Locale;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import com.eltima.components.ui.DatePicker;
import com.ruipengkj.commons.util.Util;
/**
 * 说明：
 * 日期组件
 * @Author：李智深
 * @Date：2012-03-14
 */
public class DataPackageComponent extends JPanel{

	private static final long serialVersionUID = 1L;
	
	private JLabel label;
	private DatePicker datepick;
	private static final String DefaultFormat = "yyyy-MM-dd HH:mm:ss";
	private Date date=new Date();
//	private Font font=new Font("Times New Roman", Font.BOLD, 14);
	private Dimension dimension=new Dimension(140,24);
	private int[] hilightDays={1,3,5,7};
	private int[] disabledDays={4,6,5,9};
	private boolean isInit = true;
	
	public DataPackageComponent(String lib,Date date){
		label=new JLabel(lib);
		datepick = new DatePicker(date,DefaultFormat,null,dimension);//自定义参数值
		datepick.setLocale(Locale.CHINA);//设置国家
		this.add(label);
		this.add(datepick);
		
		this.setCursor(Util.CURSOR_DEFAULT);
	}
	
	public DataPackageComponent(String lib,int size){
		this.setOpaque(false);
		label=new JLabel(lib);
		datepick = new DatePicker(new Date(),DefaultFormat,null,dimension);//自定义参数值
//		datepick.a("");
//		datepick = new DatePicker(null,DefaultFormat,null,dimension);//自定义参数值
//		datepick.a(false);
//		datepick.setName("");
		//datepick.setLocation(137, 83);
//		datepick.setHightlightdays(hilightDays, Color.red);//设置一个月份中需要高亮显示的日子
//		datepick.setDisableddays(disabledDays);//设置一个月份中不需要的日子，呈灰色显示
		datepick.setLocale(Locale.CHINA);//设置国家
		datepick.setTimePanleVisible(true);//设置时钟面板可见
		this.add(label);
		this.add(datepick);
		
		datepick.fd.setText("");
//		Document dt = datepick.fd.getDocument();
//        dt.addDocumentListener(new javax.swing.event.DocumentListener(){
//            
//            public void changedUpdate(DocumentEvent documentEvent)   {
//                System.out.println("changed:");
//                isInit = false;
//            }
//
//            public void insertUpdate(DocumentEvent documentEvent)   {
//                System.out.println("insert");
////                if(isInit && documentEvent.getLength() >=19)
////                	datepick.a((Date)null);
////                	datepick.fd.setText("");
//            }
//
//            public void removeUpdate(DocumentEvent documentEvent)   {
//               System.out.println("remove");
//               isInit = false;
//            }
//        });
        datepick.fd.addCaretListener(new CaretListener() {
			
			@Override
			public void caretUpdate(CaretEvent e) {
				isInit = false;
			}
		});
//        datepick.btn.addActionListener(this);
//        datepick.btn.doClick();
//        datepick.a((Date)null);
//        datepick.btn.doClick();
	}
	
	public DataPackageComponent(String lib,int size, String DefaultFormat){
		this.setOpaque(false);
		label=new JLabel(lib);
		datepick = new DatePicker(new Date(),DefaultFormat,null,dimension);//自定义参数值
		datepick.setLocale(Locale.CHINA);//设置国家
		datepick.setTimePanleVisible(false);//设置时钟面板可见
		this.add(label);
		this.add(datepick);
		
		datepick.fd.setText("");
        datepick.fd.addCaretListener(new CaretListener() {
			
			@Override
			public void caretUpdate(CaretEvent e) {
				isInit = false;
			}
		});
	}
	
	public DataPackageComponent(String lib,int size, boolean type){
		this.setOpaque(false);
		label=new JLabel(lib);
		datepick = new DatePicker(null,DefaultFormat,null,dimension);//自定义参数值
		datepick.setLocale(Locale.CHINA);//设置国家
		this.add(label);
		this.add(datepick);
	}
	
	public String getText(){
		return datepick.getText();
	}
	
	public Date getDate(){
//		String text=datepick.getText();
//		if(text!=null&&!text.trim().equals("")){
		if(this.isInit)
			return null;
		return (Date)datepick.getValue();
//		}
//		return null;
	}

	public DatePicker getDatePicker() {
		return this.datepick;
	}
	public void clearDatepick() {
//		Date date=null;
		datepick.a(new Date());
		datepick.fd.setText("");
		isInit = true;
	}

	public void setDate(Date date){
		if (date == null) {
			datepick.a(new Date());
			datepick.fd.setText("");
			isInit = true;
		} else {
			datepick.a(date);
		}
	}
	
	public void setEnabled(boolean flag){
		super.setEnabled(flag);
		datepick.setEnabled(flag);
	}
	
//	public void setIsInit(boolean isInit) {
//		this.isInit = isInit;
//	}
//	
//	public boolean isInit() {
//		return isInit;
//	}
	
	public static void main(String args[]) {
		
		DataPackageComponent DataPackageComponent=new DataPackageComponent("", 188);
		DataPackageComponent.getDate();
//		DataPackageComponent.getDatePicker().fd.setText("2012-12-1 10:10:10");
		System.out.println(DataPackageComponent.getDatePicker().fd.getText());
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");//Windows外观
		} catch (Exception e) {}
		
		JFrame frame = new JFrame();
		frame.setLayout(new BorderLayout());
		frame.setSize(new Dimension(300, 300));
		frame.add(DataPackageComponent);
		frame.add(new JButton("aa"), BorderLayout.NORTH);
		frame.setVisible(true);

	}

//	@Override
//	public void actionPerformed(ActionEvent arg0) {
//		datepick.a((Date)null);
//	}
}
